<template>
  <div>
    <div class="goodsType">
      <SearchBar @del="del" :buttons="buttons" @refresh="refresh" :serachData="serachData"/>
      <hr>
      <TableTreeList 
        :tableCols="tableCols"
        :tableData="tableData"
        :operates="operates"
        :enableEdit="true"
        ref="list"
        @handleSelect="handleSelect"/>
      <Pagination :pageData="pageList" @change="pageChange" />
      <addAndEditGoodsType ref="addAndEditGoodsType" @refresh="refresh"/>
    </div>
  </div>
</template>
<script>
import SearchBar from "@/components/index/SearchBar";
import TableTreeList from "@/components/index/tableTreeList";
import Pagination from "@/components/index/pagination";
import addAndEditGoodsType from "./addAndEditGoodsType";
import { getAllType,delGoodsType } from "@/js/api/manage";
export default {
  name:'goodsType',
  components:{
    SearchBar,
    Pagination,
    TableTreeList,
    addAndEditGoodsType,
  },
  data(){
    return {
      tableCols: [
        { label: "类型id", prop: "id",minWidth:'70px',sortable: true,edit:false },
        { label: "类型名称", prop: "label",minWidth:'70px',sortable: true,edit:false },
        { label: "类型值", prop: "value",minWidth:'70px',sortable: true,edit:false },
        { label: "所属父类", prop: "pid",minWidth:'70px',sortable: true,edit:false },
      ],
      tableData:[],
      operates: {
        minWidth: 100,
        list: [
          {
            id: "1",
            label: "修改",
            type: "text",
            show: true,
            disabled: false,
            size: "small",
            method: (index, row) => {
              let pObj
              if(row.pid!=0){
                pObj = this.findById(row.pid,'id')
              }else{
                pObj = {label:'root',pid:0}
              }
              this.$refs.addAndEditGoodsType.show(row,pObj)
            },
          },
          {
            id: "2",
            label: "删除",
            type: "text",
            show: true,
            disabled: false,
            size: "small",
            method: (index, row) => {
              delGoodsType(row.id).then(response=>{
                if(response.data.state){
                  this.utils.tips.success(response.data.data.msg)
                  this.loadList()
                }else{
                  this.utils.tips.error(response.data.data.msg)
                }
              })
            },
          }
        ],
      },
      buttons:[
        {
          label: "新增类型",
          icon: "el-icon-plus",
          size:'mini',
          type:"primary",
          method: () => {
            this.$refs.addAndEditGoodsType.show()
          }
        },
      ],
      pageList:{pageSize:6,pageNo:1,total:0},
      selectRows:[],
      serachData:["id","role_id","goods_name","goods_type","state"],
    }
  },
  created(){
    this.loadList()
  },
  methods:{
    del(){
      if(this.selectRows.length>0){
        this.utils.tips.confirm("确定要删除吗？",(val)=>{
          if(val){
            let ids = []
            for(let i of this.selectRows){
              ids.push(i.id)
            }
            this.deleteData(ids)
          }
        })
      }
    },
    pageChange(val){
      if(val.pageNo!=""){
        this.pageList = val
      }
      this.loadList()
    },
    loadList(){
      let obj = {
        pageSize:this.pageList.pageSize,
        pageNo:this.pageList.pageNo,
        search:this.condition
      }
      getAllType(obj).then(response=>{
        if(response.data.state){
          this.tableData = response.data.data.list
          this.pageList.pageNo = response.data.data.pageNo
          this.pageList.pageSize = response.data.data.pageSize
          this.pageList.total = response.data.data.totalRecords
        }
      })
    },
    refresh(val){
      this.condition = val
      this.loadList()
    },
    handleSelect(val){
      this.selectRows = val == undefined ? [] : val
    },
    findById(val,type,data=this.tableData){
      function getChidlren(val,type,data) {
        var hasFound = false,result = null; // 表示是否有找到id值;
        var fn = function (val,type,data) {
          if (Array.isArray(data) && !hasFound) { // 判断是否是数组并且没有的情况下，
            data.forEach(item => {
              if (item[type] === val) { // 数据循环每个子项，并且判断子项下边是否有id值
                hasFound = true; // 并且找到id值
                result = item;
              } else if (item.children) {
                fn(val,type,item.children); // 递归调用下边的子项
              }
            })
          }
          return result
        }
        return fn(val,type,data); // 调用一下
      }
      return getChidlren(val,type,data);
    }
  }
}
</script>

<style lang="less">

</style>